package com.taxi.comm.server.mina.filter;

import java.nio.charset.Charset;

import net.sf.json.JSONObject;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.codec.demux.MessageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.lang.ByteUtils;
import com.taxi.comm.business.SpringContext;
import com.taxi.comm.business.service.M004.M004Response;
//import com.taxi.comm.business.service.AbstractPMService;
//import com.taxi.comm.message.Response;
import com.taxi.comm.message.Response;
import com.taxi.comm.server.Configuration;
import com.taxi.comm.util.BytesUtils;
import com.taxi.comm.util.ZipUtils;


/**
 * Encoder for formatting the response instance to bytes stream.
 * 
 * @author Watertao
 *
 */
public class PMSMessageEncoder implements MessageEncoder<Response> {

	private static final Logger log = LoggerFactory.getLogger(PMSMessageEncoder.class);
	
	private String encoding;
	
	public PMSMessageEncoder() {
		
		Configuration config = Configuration.getInstance();
		
		encoding = config.getEncoding();
		
	}

	public void encode(IoSession session, Response message,
			ProtocolEncoderOutput out) throws Exception {
		
		String xmlString = "";
		
		if(!message.BWLX.equals("M004")){
			
			xmlString="<SHCZ Ver=\""+message.Ver+"\"><Head><BWLS>"
		     +message.BWLS+"</BWLS><BWLX>"+message.BWLX+"</BWLX><QQBZ>"
		     +message.QQBZ+"</QQBZ><YDDM>"+message.YDDM+"</YDDM><FSF>"+message.FSF+"</FSF><JSF>"
		     +message.JSF+"</JSF><SCSJ>"+message.SCSJ+"</SCSJ><BWXH>"
		     +message.BWXH+"</BWXH><XTMC>"+message.XTMC+"</XTMC></Head><RSP><YDDM>"
		     +message.RPYDDM +"</YDDM><YDXX>"+message.YDXX+"</YDXX></RSP></SHCZ>";
			
		}
		else
		{
			M004Response res=(M004Response)message;
			 xmlString="<SHCZ Ver=\""+res.Ver+"\"><Head><BWLS>"
	         +res.BWLS+"</BWLS><BWLX>"+res.BWLX+"</BWLX><QQBZ>"
	         +res.QQBZ+"</QQBZ><YDDM>"+res.YDDM+"</YDDM><FSF>"+res.FSF+"</FSF><JSF>"
	         +res.JSF+"</JSF><SCSJ>"+res.SCSJ+"</SCSJ><BWXH>"
	         +res.BWXH+"</BWXH><XTMC>"+res.XTMC+"</XTMC></Head><RSP><YDDM>"
	         +res.RPYDDM +"</YDDM><YDXX>"+res.YDXX+"</YDXX><JDPM>"+res.JDPM+"</JDPM><CPH>"
	         +res.CPH+"</CPH><YYZH>"+res.YYZH+"</YYZH><ZYZH>"+res.ZYZH+"</ZYZH><SJXM>"
	         +res.SJXM+"</SJXM><CZGS>"+res.CZGS+"</CZGS><CKJL>"+res.CKJL+"</CKJL><DDSJ>"
	         +res.DDSJ+"</DDSJ><SJDH>"+res.SJDH+"</SJDH><BZ>"+res.BZ+"</BZ></RSP></SHCZ>";
			
		}
	
			
		System.out.println("xmlString="+xmlString);	
		log.info("<Response>: {}", xmlString);
	    
		
		
		byte[] bContent = xmlString.getBytes(Charset.forName(encoding));

		boolean isCompress = false;
		if (bContent.length > Configuration.getInstance().getCompressLimit()) {
			isCompress = true;
		}
		if (isCompress) {
			bContent = ZipUtils.Zip(bContent);
		}

		IoBuffer buf = IoBuffer.allocate(bContent.length + 5);
		buf.setAutoExpand(true);
		
		buf.put(BytesUtils.intToByteArray(bContent.length));
		buf.put(isCompress ? (byte) 1 : (byte) 0);
		buf.put(bContent);
		buf.flip();

		out.write(buf);
		
		
	}

}
